class Solution {
public:
    int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
        unordered_map<int, vector<int>> g;
        for (int i = 0; i < n; i++) {
            g[manager[i]].emplace_back(i);
        }
        function<int(int)> dfs = [&](int cur) -> int {
            int res = 0;
            for (int neighbor : g[cur]) {
                res = max(res, dfs(neighbor));
            }
            return informTime[cur] + res;
        };
        return dfs(headID);
    }
};


